// 数据库表结构定义
const dbTables = {
  "客户信息表": [
    {name: "客户ID", type: "VARCHAR(20)", required: true, fieldType: "用户标识"},
    {name: "姓名", type: "VARCHAR(50)", required: true, fieldType: "用户信息"},
    {name: "省份", type: "VARCHAR(20)", required: true, fieldType: "地理信息"},
    {name: "城市", type: "VARCHAR(30)", required: true, fieldType: "地理信息"},
    {name: "注册日期", type: "DATE", required: true, fieldType: "时间信息"},
    {name: "客户等级", type: "VARCHAR(10)", required: false, fieldType: "用户属性"},
    {name: "客户价值", type: "DECIMAL(10,2)", required: false, fieldType: "业务指标"},
    {name: "联系方式", type: "VARCHAR(50)", required: true, fieldType: "联系信息"}
  ],
  "交易记录表": [
    {name: "交易ID", type: "BIGINT", required: true, fieldType: "交易标识"},
    {name: "客户ID", type: "VARCHAR(20)", required: true, fieldType: "用户标识"},
    {name: "交易金额", type: "DECIMAL(10,2)", required: true, fieldType: "交易信息"},
    {name: "交易日期", type: "DATETIME", required: true, fieldType: "时间信息"},
    {name: "支付方式", type: "VARCHAR(20)", required: false, fieldType: "交易信息"}
  ],
  "用户行为表": [
    {name: "行为ID", type: "BIGINT", required: true, fieldType: "行为标识"},
    {name: "客户ID", type: "VARCHAR(20)", required: true, fieldType: "用户标识"},
    {name: "行为类型", type: "VARCHAR(20)", required: true, fieldType: "行为属性"},
    {name: "行为时间", type: "DATETIME", required: true, fieldType: "时间信息"},
    {name: "页面路径", type: "VARCHAR(200)", required: false, fieldType: "页面信息"}
  ]
};

// 数据集字段定义
const datasetFields = {
  "1": [ // 源数据
    {name: "cust_id", type: "VARCHAR(20)", required: true, fieldType: "用户标识", changeType: "none"},
    {name: "姓名", type: "VARCHAR(50)", required: true, fieldType: "用户信息", changeType: "none"},
    {name: "电话", type:
 "VARCHAR(15)", required: false, fieldType: "联系信息", changeType: "none"},
    {name: "地址", type: "VARCHAR(200)", required: false, fieldType: "地理信息", changeType: "none"},
    {name
: "注册日期", type: "DATE", required: true, fieldType: "时间信息", changeType: "none"}
  ],
  "2": [ // 清洗转换
    {name: "customer_id", type: "VARCHAR(20)", required: true, fieldType: "用户标识", changeType: "modified"},
    {name: "full_name", type: "VARCHAR(50)", required: true, fieldType: "用户信息", changeType: "modified"},
    {name: "province", type: "VARCHAR(20)", required: true, fieldType: "地理信息", changeType: "split"},
    {name: "city", type: "VARCHAR(30)", required: true, fieldType: "地理信息", changeType: "split"},
    {name: "register_date", type: "DATE", required: true, fieldType: "时间信息", changeType: "modified"},
    {name: "电话", type: "VARCHAR(15)", required: false, fieldType: "联系信息", changeType: "removed", isRemoved: true}
  ],
  "3": [ // 数据增强
    {name: "customer_id", type: "VARCHAR(20)", required: true, fieldType: "用户标识", changeType: "none"},
    {name: "full_name", type: "VARCHAR(50)", required: true, fieldType: "用户信息", changeType: "none"},
    {name: "province", type: "VARCHAR(20)", required: true, fieldType: "地理信息", changeType: "none"},
    {name: "city", type: "VARCHAR(30)", required: true, fieldType: "地理信息", changeType: "none"},
    {name: "register_date", type: "DATE", required: true, fieldType: "时间信息", changeType: "none"},
    {name: "loyalty_level", type: "VARCHAR(10)", required: false, fieldType: "用户属性", changeType: "added"},
    {name: "customer_value", type: "DECIMAL(10,2)", required: false, fieldType: "业务指标", changeType: "aggregate"}
  ]
};

// 预览数据
const previewData = {
  "1": { // 源数据
    title: "源数据表 - 实际数据预览",
    headers: ["cust_id", "姓名", "电话", "地址", "注册日期"],
    columnChanges: {
      "地址": "modified",
      "姓名": "modified"
    },
    rows: [
      ["C1001", "张三", "13800138000", "北京市海淀区中关村大街1号", "2020-05-12"],
      ["C1002", "李四", "13900139000", "上海市浦东新区张江高科技园区", "2021-03-22"],
      ["C1003", "王五", "13700137000", "广州市天河区珠江新城", "2019-11-15"],
      ["C1004", "赵六", "13500135000", "深圳市福田区华强北路", "2020-08-09"],
      ["C1005", "钱七", "13600136000", "杭州市西湖区文三路", "2021-07-15"],
      ["C1006", "孙八", "13700137001", "南京市鼓楼区中山路", "2022-01-10"],
      ["C1007", "周九", "13800138002", "成都市武侯区天府大道", "2021-09-05"],
      ["C1008", "吴十", "13900139003", "武汉市武昌区长江大道", "2019-05-22"]
    ],
    stats: {
      totalRecords: "192K",
      columns: "5",
      quality: "98.7%"
    }
  },
  "2": { // 清洗转换
    title: "清洗转换表 - 实际数据预览",
    headers: ["customer_id", "full_name", "province", "city", "register_date"],
    columnChanges: {
      "customer_id": "modified",
      "full_name": "aggregate",
      "province": "split",
      "city": "split",
      "register_date": "modified"
    },
    rows: [
      ["C1001", "张三", "北京", "北京市", "2020-05-12"],
      ["C1002", "李四", "上海", "上海市", "2021-03-22"],
      ["C1003", "王五", "广东", "广州市", "2019-11-15"],
      ["C1004", "赵六", "广东", "深圳市", "2020-08-09"],
      ["C1005", "钱七", "浙江", "杭州市", "2021-07-15"],
      ["C1006", "孙八", "江苏", "南京市", "2022-01-10"],
      ["C1007", "周九", "四川", "成都市", "2021-09-05"],
      ["C1008", "吴十", "湖北", "武汉市", "2019-05-22"]
    ],
    stats: {
      totalRecords: "192K",
      columns: "5",
      quality: "99.2%"
    }
  },
  "3": { // 数据增强
    title: "增强数据表 - 实际数据预览",
    headers: ["customer_id", "full_name", "province", "city", "register_date", "loyalty_level", "customer_value"],
    columnChanges: {
      "loyalty_level": "added",
      "customer_value": "aggregate"
    },
    rows: [
      ["C1001", "张三", "北京", "北京市", "2020-05-12", "金卡", "15842.50"],
      ["C1002", "李四", "上海", "上海市", "2021-03-22", "银卡", "7820.40"],
      ["C1003", "王五", "广东", "广州市", "2019-11-15", "钻石卡", "28570.80"],
      ["C1004", "赵六", "广东", "深圳市", "2020-08-09", "普通", "5320.90"],
      ["C1005", "钱七", "浙江", "杭州市", "2021-07-15", "白金卡", "18720.30"],
      ["C1006", "孙八", "江苏", "南京市", "2022-01-10", "普通", "3250.60"],
      ["C1007", "周九", "四川", "成都市", "2021-09-05", "银卡", "9840.20"],
      ["C1008", "吴十", "湖北", "武汉市", "2019-05-22", "金卡", "12680.70"]
    ],
    stats: {
      totalRecords: "192K",
      columns: "7",
      quality: "99.5%"
    }
  }
};

// 转换详情数据
const transformDetails = {
  overview: [
    { label: "列数量", value: "7" },
    { label: "总记录数", value: "192K" },
    { label: "质量评分", value: "98.7%" },
    { label: "处理步骤", value: "3" }
  ],
  columns: [
    { name: "customer_id", fieldType: "用户标识", required: "是", dataType: "string(10)", source: "原始列: cust_id" },
    { name: "full_name", fieldType: "用户信息", required: "是", dataType: "string(50)", source: "原始列: 姓名" },
    { name: "province", fieldType: "地理信息", required: "是", dataType: "string(20)", source: "拆分列 (从地址拆分)" },
    { name: "city", fieldType: "地理信息", required: "是", dataType: "string(30)", source: "拆分列 (从地址拆分)" },
    { name: "register_date", fieldType: "时间信息", required: "是", dataType: "date", source: "原始列: 注册日期" },
    { name: "loyalty_level", fieldType: "用户属性", required: "否", dataType: "string(10)", source: "新增列 (基于交易计算)" },
    { name: "customer_value", fieldType: "业务指标", required: "否", dataType: "float", source: "聚合列 (交易聚合值)" }
  ]
};

// 字段映射关系
const fieldMappings = {
  'cust_id': '客户ID',
  'customer_id': '客户ID',
  'full_name': '姓名',
  'province': '省份',
  'city': '城市',
  'register_date': '注册日期',
  'loyalty_level': '客户等级',
  'customer_value': '客户价值',
  '电话': '联系方式'
};

// 流水线步骤定义
const pipelineSteps = [
  {
    id: "1",
    name: "源数据表",
    description: "原始客户数据",
    icon: "fas fa-database",
    color: "var(--step-1-color)"
  },
  {
    id: "2",
    name: "清洗转换表",
    description: "清洗后客户数据",
    icon: "fas fa-filter",
    color: "var(--step-2-color)"
  },
  {
    id: "3",
    name: "增强数据表",
    description: "添加衍生特征",
    icon: "fas fa-chart-line",
    color: "var(--step-3-color)"
  }
];

// 图例定义
const legends = [
  { text: "新增字段", iconClass: "icon-added", icon: "fas fa-plus" },
  { text: "修改字段", iconClass: "icon-modified", icon: "fas fa-sync-alt" },
  { text: "删除字段", iconClass: "icon-removed", icon: "fas fa-trash" },
  { text: "拆分操作", iconClass: "icon-split", icon: "fas fa-cut" },
  { text: "聚合操作", iconClass: "icon-aggregate", icon: "fas fa-object-group" }
];

// 创建响应式状态
import { reactive } from 'vue';

// 创建状态存储
export const store = {
  state: reactive({
    currentStep: "1",
    selectedTable: "客户信息表",
    dbTables,
    datasetFields,
    previewData,
    transformDetails,
    fieldMappings,
    pipelineSteps,
    legends,
    activeTab: "数据集结构"
  }),

  // 方法
  setCurrentStep(stepId) {
    this.state.currentStep = stepId;
  },

  setSelectedTable(tableName) {
    this.state.selectedTable = tableName;
  },

  setActiveTab(tabName) {
    this.state.activeTab = tabName;
  },

  // 查找匹配字段
  findMatchingField(datasetField, tableFields) {
    // 尝试直接匹配或通过映射匹配
    return tableFields.find(field =>
      field.name === datasetField.name ||
      field.name === this.state.fieldMappings[datasetField.name] ||
      datasetField.name === this.state.fieldMappings[field.name]
    );
  },

  // 计算匹配率
  calculateMatchRate(currentFields, tableFields) {
    const matchCount = currentFields.filter(f => this.findMatchingField(f, tableFields)).length;
    return {
      rate: Math.round((matchCount / currentFields.length) * 100),
      count: matchCount,
      total: currentFields.length
    };
  }
};
